package com.neusoft.crm.service.impl;

import com.neusoft.crm.mapper.BasicDataMapper;
import com.neusoft.crm.po.DataArea;
import com.neusoft.crm.po.DataClientLevel;
import com.neusoft.crm.po.DataServerType;
import com.neusoft.crm.service.IBasicDataService;
import com.neusoft.crm.utils.Slf4j2Logger;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 基础数据管理实现类
 *
 * @author lanan1207
 */
@Service
public class BasicDataServiceImpl implements IBasicDataService {
    @Autowired
    BasicDataMapper basicDataMapper;

    @Override
    @Cacheable(value = "AreaInfo", key = "'AreaList'", unless = "#result==null")
    public List<DataArea> selectRegionList(DataArea dataArea) {
        return basicDataMapper.selectRegionList(dataArea);
    }

    @Override
    @CacheEvict(value = "AreaInfo", allEntries = true)
    public int addRegionInfo(DataArea dataArea) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = basicDataMapper.addRegionInfo(dataArea);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("地区信息新增失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @CacheEvict(value = "AreaInfo", allEntries = true)
    public int updateRegionInfo(DataArea dataArea) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = basicDataMapper.updateRegionInfo(dataArea);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("地区信息更新失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @CacheEvict(value = "AreaInfo", allEntries = true)
    public int deleteRegionInfo(DataArea dataArea) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = basicDataMapper.deleteRegionInfo(dataArea);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("地区信息删除失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @Cacheable(value = "LevelInfo", key = "'LevelList'", unless = "#result==null")
    public List<DataClientLevel> selectCustomerLevelList(DataClientLevel dataClientLevel) {
        return basicDataMapper.selectCustomerLevelList(dataClientLevel);
    }

    @Override
    @CacheEvict(value = "LevelInfo", allEntries = true)
    public int addCustomerLevel(DataClientLevel dataClientLevel) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = basicDataMapper.addCustomerLevel(dataClientLevel);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("客户等级更新失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @CacheEvict(value = "LevelInfo", allEntries = true)
    public int updateCustomerLevel(DataClientLevel dataClientLevel) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = basicDataMapper.updateCustomerLevel(dataClientLevel);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("客户等级更新失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @CacheEvict(value = "LevelInfo", allEntries = true)
    public int deleteCustomerLevel(DataClientLevel dataClientLevel) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = basicDataMapper.deleteCustomerLevel(dataClientLevel);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("客户等级更新失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @Cacheable(value = "ServerTypeInfo", key = "'ServerTypeList'", unless = "#result==null")
    public List<DataServerType> selectServiceTypeList(DataServerType dataServerType) {
        return basicDataMapper.selectServiceTypeList(dataServerType);
    }

    @Override
    @CacheEvict(value = "ServerTypeInfo", allEntries = true)
    public int addServiceType(DataServerType dataServerType) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = basicDataMapper.addServiceType(dataServerType);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("服务类型更新失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @CacheEvict(value = "ServerTypeInfo", allEntries = true)
    public int updateServiceType(DataServerType dataServerType) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = basicDataMapper.updateServiceType(dataServerType);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("服务类型更新失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @Override
    @CacheEvict(value = "ServerTypeInfo", allEntries = true)
    public int deleteServiceType(DataServerType dataServerType) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = basicDataMapper.deleteServiceType(dataServerType);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("服务类型更新失败，失败原因为：", e);
        }
        return influenceLineCount;
    }
}
